package com.sevenyear.excelanalyze.listener.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sevenyear.excelanalyze.common.utils.CalculateUtils;
import com.sevenyear.excelanalyze.entity.EvaporationRate;
import com.sevenyear.excelanalyze.entity.HydroelectricConversionRate;
import com.sevenyear.excelanalyze.entity.PartitionBaseInfo;
import com.sevenyear.excelanalyze.service.EvaporationRateService;
import com.sevenyear.excelanalyze.service.HydroelectricConversionRateService;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;

/**
 * @author ：SevenYear
 * @description：TODO
 * @date ：2021/8/29 21:59
 */
public class HydroelectricConversionRateListener extends AnalysisEventListener<HydroelectricConversionRate> {

    private HydroelectricConversionRateService hydroelectricConversionRateService;
    private PartitionBaseInfoService partitionBaseInfoService;

    public HydroelectricConversionRateListener() {
    }

    public HydroelectricConversionRateListener(HydroelectricConversionRateService hydroelectricConversionRateService, PartitionBaseInfoService partitionBaseInfoService) {
        this.hydroelectricConversionRateService = hydroelectricConversionRateService;
        this.partitionBaseInfoService = partitionBaseInfoService;
    }

    @Override
    public void invoke(HydroelectricConversionRate hydroelectricConversionRate, AnalysisContext analysisContext) {
        //计算平均值
        float average = CalculateUtils.average(hydroelectricConversionRate.getHydroelectricConversionRateMin(), hydroelectricConversionRate.getHydroelectricConversionRateMax());
        hydroelectricConversionRate.setHydroelectricConversionRateAvg(average);
        //设置分区主键id,分区编码partitionCode,区域编码areaCode
        QueryWrapper<PartitionBaseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("landform", hydroelectricConversionRate.getLandform()).eq("clay_thickness", hydroelectricConversionRate.getClayThickness());
        PartitionBaseInfo partitionBaseInfo = partitionBaseInfoService.getOne(wrapper);
        hydroelectricConversionRate.setPartitionId(partitionBaseInfo.getId());
        hydroelectricConversionRate.setPartitionCode(partitionBaseInfo.getPartitionCode());
        hydroelectricConversionRate.setAreaCode(hydroelectricConversionRate.getArea() + hydroelectricConversionRate.getPartitionCode());
        UpdateWrapper<HydroelectricConversionRate> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("area_code", hydroelectricConversionRate.getAreaCode());
        hydroelectricConversionRateService.saveOrUpdate(hydroelectricConversionRate, updateWrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
